<template>
  <div class="layout-container">
    <div class="layout-container-form" style="flex-direction: column">
      <div class="layout-container-form-search">
        <el-row style="text-align: left">
          <el-col :span="24">
            <el-form label-width="90px" :inline="true">
              <el-form-item label="时间段：">
                <el-date-picker
                  v-model="query.date_time_range"
                  type="datetimerange"
                  prefix-icon="el-icon-date"
                  range-separator="→"
                  value-format="YYYY-MM-DD HH:mm:ss"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  style="width: 100%"
                />
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  class="search-btn"
                  @click="getTableData(true)"
                  >{{ $t("message.common.search") }}</el-button
                >
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
      <div class="layout-container-opt">
        <el-row>
          <el-col :span="24" >
            <el-button
              type="primary"
              :disabled="project_ids.length === 0"
              @click="delOpt(1)"
            >
              批量删除项目
            </el-button>
            <el-button
              type="primary"
              :disabled="project_ids.length === 0"
              @click="delOpt(2)"
            >
              批量删除所选时间段发放的题目
            </el-button>
            <el-button
              type="primary"
              :disabled="project_ids.length === 0"
              @click="delOpt(3)"
            >
              批量删除所选时间段发放题目的答案
            </el-button>
            <el-button
              type="primary"
              :disabled="project_ids.length === 0"
              @click="delOpt(4)"
            >
              批量删除所选项目发送的通知
            </el-button>
          </el-col>
        </el-row>
      </div>
    </div>
    <div class="layout-container-table">
      <Table
        ref="table"
        v-model:page="page"
        v-loading="loading"
        :showSelection="true"
        :data="tableData"
        @getTableData="getTableData"
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column prop="id" label="项目ID" align="center" width="80" /> -->
        <el-table-column
          prop="img"
          label="项目"
        >
          <template #default="scope">
						<div class="img-and-text">
              <div>
                <el-image
                  v-if="scope.row.cover"
                  style="width: 80px; height: 50px; cursor: pointer;"
                  :src="scope.row.cover"
                  :preview-src-list="[scope.row.cover]"
                />
                <div
                  v-else
                  class="no-img"
                >
                  暂无图片
                </div>
              </div>
              <div class="project-info">
                <div class="project-title">
                  {{scope.row.title}}
                </div>
                <div class="project-count">
                  题目数：{{scope.row.topic_count}}
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="rule_value" label="答案数据" width="200">
          <template #default="scope">
            <div>
              答案数据
            </div>
            <div>
              {{scope.row.answer_data}}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="rule_value" label="回答题数" width="200">
          <template #default="scope">
            <div>
              回答题数
            </div>
            <div>
             {{scope.row.answer_count}}/{{scope.row.topic_count}}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('message.common.handle')"
          align="center"
          fixed="right"
          width="200"
        >
          <template #default="scope">
            <el-button type="text" class="opt-del">
              <el-button type="text">
              <el-dropdown>
                <span class="el-dropdown-link" style="color:#409eff;font-size: 12px;" @mouseover="showArrow" @mouseenter="showArrow">
                  {{ $t("message.common.deleteData") }} <i v-if="!showArrowFlag" class="el-icon-arrow-down"></i><i v-else class="el-icon-arrow-up"></i>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="handleOpt(scope.row, 2)">删除所有时间段发放的题目</el-dropdown-item>
                    <el-dropdown-item @click="handleOpt(scope.row, 3)">删除所有时间段发放题目的答案</el-dropdown-item>
                    <el-dropdown-item @click="handleOpt(scope.row, 1)">删除项目</el-dropdown-item>
                    <el-dropdown-item @click="handleOpt(scope.row, 4)">删除项目通知</el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </el-button>
            </el-button>
          </template>
        </el-table-column>
      </Table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { defineComponent, ref, reactive } from "vue";
import { Page } from "@/components/table/type";
import { ElMessage, ElMessageBox } from "element-plus";
import Table from "@/components/table/index.vue";
import { getDataManageList, delDataMessageProject, delDataMessageTopic, delDataMessageTopicAnswer, delDataMessage } from "@/api/service/dataManagent.ts";
import { useStore } from 'vuex';
const store = useStore();
const adminInfo = reactive(store.state.user.adminInfo);
// 存储搜索用的数据
const query = reactive({
  date_time_range: [], // 上线时间
});

// 分页参数, 供table使用
const page: Page = reactive({
  index: 1,
  size: 50,
  total: 0,
});
const loading = ref(false);
const tableData = ref([]);
// 获取表格数据
// params <init> Boolean ，默认为false，用于判断是否需要初始化分页
const getTableData = (init: Boolean) => {
  loading.value = true;
  if (init) {
    page.index = 1;
  }
  let params = {
    page: page.index,
    page_size: page.size,
    date_time_range: query.date_time_range ? query.date_time_range : [],
  };
  getDataManageList(params)
    .then((res: any) => {
      let data = res.data.list;
      data.forEach((d: any) => {
        d.loading = false;
      });

      tableData.value = data;
      page.total = Number(res.data.count);
    })
    .catch(() => {
      tableData.value = [];
      page.index = 1;
      page.total = 0;
    })
    .finally(() => {
      loading.value = false;
    });
};


// 勾选数据
const project_ids = ref([])
const handleSelectionChange = (data: any) => {
  console.log(data)
  project_ids.value = []
  data.forEach((item: any) => {
    project_ids.value.push(item.project_id)
  });
};
// 操作按钮点击事件 
const showArrowFlag = ref(false)
const showArrow = () => {
  showArrowFlag.value = !showArrowFlag.value 
}
// 删除按钮
const delOpt = (type: Number) => {
   ElMessageBox.confirm(
    `是否确认当前操作?`,
    '',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'success',
    }
  )
  .then(() => {
      deleteFun(project_ids.value, type)
  })
  .catch(() => {
    
  })
}


// 编辑
const handleOpt = (row: any, type: Number) => {
    ElMessageBox.confirm(
    `是否确认当前操作?`,
    '',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'success',
    }
  )
  .then(() => {
    let ids = [row.project_id]
    deleteFun(ids, type)
  })
  .catch(() => {
    
  })
}

// 删除函数
const deleteFun = (ids: any, type: Number) => {
  console.log(query.date_time_range)
  query.date_time_range = query.date_time_range ? query.date_time_range : []
  let data = {
    project_ids: ids,
    start_date_time: query.date_time_range[0] || undefined,
    end_date_time: query.date_time_range[1] || undefined
  };
  if (type === 1) {
    // 批量删除项目
    delDataMessageProject(data)
      .then((res: any) => {
        if (res.code == 200) {
          getTableData(false);
          ElMessage.success("删除成功！");
        } else {
          ElMessage.error("删除失败！");
        }
      })
  	.catch(() => false);
  }
  if (type === 2) {
    // 批量删除所选时间段发放的题目
    delDataMessageTopic(data)
  	.then((res: any) => {
  		if (res.code == 200) {
  			getTableData(false);
  			ElMessage.success("删除成功！");
  		} else {
  			ElMessage.error("删除失败！");
  		}
  	})
  	.catch(() => false);
  }
  if (type === 3) {
    // 批量删除所选时间段发放的题目答案
    delDataMessageTopicAnswer(data)
  	.then((res: any) => {
  		if (res.code == 200) {
  			getTableData(false);
  			ElMessage.success("删除成功！");
  		} else {
  			ElMessage.error("删除失败！");
  		}
  	})
  	.catch(() => false);
  }
  if (type === 4) {
    // 批量删除所选项目的通知数据
    delDataMessage(data)
      .then((res: any) => {
        if (res.code == 200) {
          getTableData(false);
          ElMessage.success("删除成功！");
        } else {
          ElMessage.error("删除失败！");
        }
      })
      .catch(() => false);
  }
};


getTableData(true);
</script>

<style lang="scss" scoped>
.layout-container-form {
  position: relative;
}
.layout-container-form-search {
  width: 100%;
  display: inline-block !important;
  justify-content: flex-start !important;
  .search-btn{
    margin-right: 10px;;
  }
}
.project-info{
  text-align: left;
  .project-title{
    font-weight: bold;
    font-size: 14px;
  }
  .project-count{
    font-weight: bold;
    font-size: 14px;
  }
}
.layout-container-opt {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  padding: 10px;
  margin-top: 10px;
}
.split-line{
  margin: 0 8px;
  color: #ccc;
}
.image-slot{
  width: 50px;
  height: 50px;
  background: #fafafa;
  color: #ccc;
  font-size: 12px;
  cursor: pointer;
}
.img-and-text{
  display: flex;
  align-items: center;
  .project-info{
    margin-left:10px;
  }
  .no-img{
    color: #ccc; 
    display: flex; 
    width: 80px;
    height: 50px;
    align-items:center;
    justify-content: center;
    background: #fafafa;
  }
}
</style>
